// pages/shop/shop.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopDesc: '',
    shopId: '',
    type: '',
    projectList: '',
    goodsList: ''
  },

  navService() {
    wx.$nav('/pages/service/service', {
      shopId: this.data.shopId
    })
  },

  prevScrollImgs(e) {
    wx.previewImage({
      current: e.currentTarget.dataset.src,
      urls: this.data.scrollImgs,
    })
  },

  getDetail() {
    wx.$https('shop.desc', {
      shopId: this.data.shopId
    }).then(res => {
      let scrollImgs = []
      scrollImgs.push(res.listImage)
      let scrollImages = res.scrollImages ? res.scrollImages.split(',') : []
      this.setData({
        shopDesc: res,
        scrollImgs: scrollImgs.concat(scrollImages)
      })
      wx.setNavigationBarTitle({ //设置标题
        title: res.typeDesc,
      })
      this.getCouponInfo()
      this.getList(0)
    })
  },

  getCouponInfo() {
    wx.$https('coupon.list', {
      start: 0,
      limit: 10,
      shopId: this.data.shopId
    }).then(res => {
      this.setData({
        coupons: res
      })
    })
  },

  navGetCoupon() {
    wx.$nav('/pages/getCoupon/getCoupon', {
      // list: JSON.stringify(this.data.coupons),
      shopId: this.data.shopId
    })
  },

  getPros(start) {
    wx.$https('project.list', {
      start: start,
      limit: 10,
      shopId: this.data.shopId
    }).then(res => {
      this.setData({
        projectList: start == 0 ? res : this.data.projectList.concat(res)
      })
    })
  },

  getGoods(start) {
    wx.$https('goods.list', {
      start: start,
      limit: 10,
      shopId: this.data.shopId,
      type: 2
    }).then(res => {
      this.setData({
        goodsList: start == 0 ? res : this.data.goodsList.concat(res)
      })
    })
  },

  col(e) {
    let id = e.currentTarget.dataset.id
    wx.$https('shop.col', {
      shopId: id
    }).then(res => {
      let desc = this.data.shopDesc
      desc.isCol = res.result
      this.setData({
        shopDesc: desc,
        type: desc.type
      })
    })
  },

  navDetail(e) {
    let id = e.currentTarget.dataset.id
    wx.$nav('/pages/shopDetail/shopDetail', {
      projectId: id
    })
  },

  navGoodsDetail(e) {
    let id = e.currentTarget.dataset.id
    wx.$nav('/pages/workerDetail/workerDetail', {
      goodsId: id
    })
  },

  getList(start) {
    if (this.data.shopDesc.type == 2) {
      this.getPros(start == 0 ? 0 : this.data.projectList.length)
    } else {
      this.getGoods(start == 0 ? 0 : this.data.goodsList.length)
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      shopId: options.shopId
    })
    this.getDetail()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getList('下拉')
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})